	set more off
 	global directory "/Users/lcarvalh/Dropbox/Meniga-Iceland Project/FOR PUBLICATION/data"
 
	**************************************************
	* VIOLATIONS of FIRST-ORDER STOCHASTIC DOMINANCE *
	**************************************************
	
	use "$directory/raw_choices.dta" if avoidance_option == 1, clear	
	
	***************************
	* CHOICES in heads-tails space
	***************************	
		
	gen x = .
	gen y = .
		replace x = 	(risk_A * risk_tails_A) + (risk_B * risk_tails_B)	if budget_order <= 8
		replace y = 	(risk_A * risk_heads_A) + (risk_B * risk_heads_B)	if budget_order <= 8 
		replace x = 	(risk_A * risk_tails_A) + (risk_B * risk_tails_B) + (risk_C * risk_tails_C) + ///
							(risk_D * risk_tails_D) + (risk_E * risk_tails_E)	if budget_order >  8
		replace y = 	(risk_A * risk_heads_A) + (risk_B * risk_heads_B) + (risk_C * risk_heads_C) + ///
							(risk_D * risk_heads_D) + (risk_E * risk_heads_E)	if budget_order >  8 	
	
	egen risk_tails = rmax(risk_tails_A risk_tails_B risk_tails_C risk_tails_D risk_tails_E)
	gen  x_max = risk_tails * 500
	
	egen risk_heads = rmax(risk_heads_A risk_heads_B risk_heads_C risk_heads_D risk_heads_E)
	gen  y_max = risk_heads * 500
	
	
	drop risk_heads risk_tails	

	ren risk_outside_option outside_option

	gen risk_free = (y_max * x_max - 160000) / (y_max + x_max + 800)
	replace risk_free = risk_free + 400
	replace outside_option = outside_option + 400
	
	***************************
	* Shifting to first quadrant
	***************************	
		
	foreach var in x y x_max y_max {
		replace `var' = `var' + 400
	}
	
		gen FOSD = .			
		gen y_prime = .
		gen x_prime = .
		
		**************************************
		***   Risk-free > Outside Option   ***
		**************************************

		// chooses outside option when it is dominated by the risk-free portfolio
		replace FOSD 	= outside_option/risk_free		if 					  outside_option < risk_free & avoidance == 1 
		
		// y/heads has highest return
		// y < x
		replace y_prime = y_max * (1 - (y/x_max))		if y_max > x_max	& outside_option < risk_free & avoidance == 0	& y <  x	 
		replace FOSD 	= (x + y) / (y 		 + y_prime)	if y_max > x_max 	& outside_option < risk_free & avoidance == 0	& y <  x	 
		replace FOSD 	= 1								if y_max > x_max 	& outside_option < risk_free & avoidance == 0	& y >= x	 

		// x/tails has highest return
		// x < y
		replace x_prime = x_max * (1 - (x/y_max))		if y_max < x_max	& outside_option < risk_free & avoidance == 0	& y >  x
		replace FOSD 	= (x + y) / (x_prime + x)		if y_max < x_max 	& outside_option < risk_free & avoidance == 0	& y >  x	 
		replace FOSD 	= 1								if y_max < x_max 	& outside_option < risk_free & avoidance == 0	& y <= x	 

		// heads and tails have same return
		// No potential for violations (b/c all portfolios yield the same expected return and the budget constraint is satisfied)
		replace FOSD 	= 1								if y_max == x_max	& outside_option < risk_free & avoidance == 0	

		**************************************
		***   Risk-free < Outside Option   ***
		**************************************

		// chooses outside option when it dominates the risk-free portfolio
		replace FOSD 	= 1			if 					  outside_option > risk_free & avoidance == 1
		
		gen den = .
		
		// y/heads has highest return
		// "right" side of the 45 degree line
		replace FOSD 	= 1								if y_max > x_max 	& outside_option > risk_free & avoidance == 0	& y >= x	& x + y >= 2 * 	outside_option 
		replace FOSD 	= 0.5*(x + y)/outside_option	if y_max > x_max 	& outside_option > risk_free & avoidance == 0	& y >= x	& x + y <  2 * 	outside_option 

		// "wrong" side of the 45 degree line
		replace y_prime = y_max * (1 - (y/x_max))		if y_max > x_max	& outside_option > risk_free & avoidance == 0	& y <  x	 
		replace den     = y + y_prime					if y_max > x_max	& outside_option > risk_free & avoidance == 0	& y <  x	& y + y_prime >= 2 * outside_option	 
		replace den     = 2 * outside_option			if y_max > x_max	& outside_option > risk_free & avoidance == 0	& y <  x	& y + y_prime <  2 * outside_option	 
		replace FOSD 	= (x + y) / den					if y_max > x_max 	& outside_option > risk_free & avoidance == 0	& y <  x	& y <  x	 
		
		// x/tails has highest return
		// "right" side of the 45 degree line		
		replace FOSD 	= 1								if y_max < x_max 	& outside_option > risk_free & avoidance == 0	& y <= x	& x + y >= 2 * 	outside_option	 
		replace FOSD 	= 0.5*(x + y)/outside_option	if y_max < x_max 	& outside_option > risk_free & avoidance == 0	& y <= x	& x + y <  2 * 	outside_option	 
				
		// "wrong" side of the 45 degree line
		replace x_prime = x_max * (1 - (x/y_max))		if y_max < x_max	& outside_option > risk_free & avoidance == 0	& y >  x
		replace den 	= x_prime + x					if y_max < x_max 	& outside_option > risk_free & avoidance == 0	& y >  x	& x_prime + x >= 2 * outside_option	 
		replace den 	= 2 * outside_option			if y_max < x_max 	& outside_option > risk_free & avoidance == 0	& y >  x	& x_prime + x <  2 * outside_option	 
		replace FOSD 	= (x + y) / den					if y_max < x_max 	& outside_option > risk_free & avoidance == 0	& y >  x	 
		
		// heads and tails have same return
		// No potential for violations (b/c all portfolios yield the same expected return and the budget constraint is satisfied)
		replace FOSD 	= risk_free/outside_option		if y_max == x_max	& outside_option > risk_free & avoidance == 0	


		collapse (mean) FOSD, by(prim_key)
		ren FOSD risk_FOSD
		
		label variable risk_FOSD "FOSD risk task (w/ outside option)"
		
		tempfile FOSD_Violations
		
		sort prim_key
		save `FOSD_Violations'
		
	**************************************************
	* 				INTERTEMPORAL CHOICES 			 *
	**************************************************
	
	use prim_key budget_order time_sooner time_endowment time_r time_complete using "$directory/raw_choices.dta" if budget_order <= 12 & time_complete == 1, clear

	
	// Fraction of Endowment Allocated to Sooner
	preserve
	
		drop if budget_order <= 7

		gen impatience = time_sooner / time_endowment
	
	
		collapse (mean) impatience, by(prim_key)
		
		label variable impatience "% allocated to sooner date"
		
		
		tempfile impatience
		
		
		sort prim_key
		save `impatience'
	
	restore
	
	
	// Present Bias
	preserve
	
		keep prim_key budget_order time_sooner time_endowment
		
		reshape wide time_sooner time_endowment, i(prim_key) j(budget_order)
		
		forvalues i = 1/6 {
			local j = `i' + 6
			gen PB`i' = (time_sooner`i' - time_sooner`j')/time_endowment`i'			
			
		}
		
		egen presentbias = rowmean(PB1 PB2 PB3 PB4 PB5 PB6)
	

		keep prim_key presentbias 
		label variable presentbias "Present bias" 
		
		
		tempfile presentbias
		
		sort prim_key 
		save `presentbias'
	
	restore	

	// Intertemporal Elasticity of Substitution
	preserve
	
		gen Y = 1 - (time_sooner/time_endowment)
		gen X = 100 * time_r

		keep prim_key budget_order X Y
	
		reshape wide Y X, i(prim_key) j(budget_order)
	
		forvalues i = 1/5 {
			local j = `i' + 1
			gen IES`i' = (Y`j' - Y`i')/(X`j' - X`i')
		}
		
		forvalues i = 7/11 {
			local j = `i' + 1
			gen IES`i' = (Y`j' - Y`i')/(X`j' - X`i')
		}
			
		egen IES = rowmean(IES*)		
		
		keep prim_key IES 
		label variable IES "Intertemporal Elasticity of Substitution" 
		
		
		tempfile IES 
		
		
		sort prim_key 
		save `IES'
	
	restore
	
	
	
	use prim_key begintime MENIGA_ID budget_order avoidance_option educ using "$directory/raw_choices.dta" if budget_order == 1, clear
		
	drop budget_order
	
	
	merge 1:1 prim_key using `FOSD_Violations', nogen
	
	merge 1:1 prim_key using `impatience', nogen
	
	merge 1:1 prim_key using `presentbias', nogen

	merge 1:1 prim_key using `IES', nogen
	
	merge 1:1 prim_key using `presentbias', nogen
	
	merge 1:1 prim_key using "$directory/CCEI.dta", nogen
	
	/*************************************************************************
	* DMQ Index															*
	*************************************************************************/
	
	foreach var in risk_FOSD risk_FOSD_GARP amb_GARP {
	
		xtile temp = `var', nquantiles(99)
		drop `var'
		ren temp `var'
		replace `var' = `var'/10
	
	}	
		/*************************************************************************
		* Hybrid measure of risk: 												 *
		*************************************************************************/
		
		gen risk_DMA = .
			replace risk_DMA = risk_FOSD_GARP 	if avoidance_option == 0
			replace risk_DMA = risk_FOSD		if avoidance_option == 1
					
								
	pca  risk_DMA amb_GARP 				 
	predict DMA if e(sample) 					 		
	
	label variable DMA "Decision-making Ability"
	
	
	keep MENIGA_ID prim_key DMA IES impatience presentbias educ begintime
	
	order MENIGA_ID prim_key DMA IES impatience presentbias educ begintime
	
				
	sort MENIGA_ID
	save "$directory/survey_data.dta", replace
